/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.edu.kamai.xmlparser.speaking;

import com.edu.kamai.xml.beans.paperXML;
import com.edu.kamai.xml.beans.speaking.speakingQuestion;
import com.edu.kamai.xml.beans.speaking.speakingXML;
import com.edu.kamai.xml.beans.writing.writingXML;
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 *
 * @author xyzhu
 */
public class speakingPaperParser {
    public speakingPaperParser(String fn){
        fileName = fn;
    }
    
    public speakingXML parseSpeakingXML(){
        paperXML pxml = new speakingXML();
        try {
            File xmlFile = new File(fileName);
            if(xmlFile.exists()){
                DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
                DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
                Document doc = dBuilder.parse(xmlFile);
                doc.getDocumentElement().normalize();
                
                int id = Integer.parseInt(doc.getDocumentElement().getAttribute("paperRoleID"));
                pxml.setPaperRoleID(id);
                
                System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
                System.out.println(doc.getDocumentElement().getAttribute("paperRoleID"));
                NodeList nList = doc.getElementsByTagName("speakingQestion");
                System.out.println("-----------------------");
                
                for (int temp = 0; temp < nList.getLength(); temp++) {
                    Node nNode = nList.item(temp);
		    if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                        Element eElement = (Element) nNode;
                        System.out.println("timeLimit: " + getTagValue("prepareTimeLimit", eElement));
                        System.out.println("paperDescription : " + getTagValue("readingTimeLimit", eElement));
                        System.out.println("mp3Path : " + getTagValue("mp3Path", eElement));
                        System.out.println("MainTxt : " + getTagValue("MainTxt", eElement));
                        System.out.println("paperTitle : " + getTagValue("paperTitle", eElement));
                        
                        String prepareTime = getTagValue("prepareTimeLimit", eElement);
                        String readingTime = getTagValue("readingTimeLimit", eElement);
                        String mp3Path = getTagValue("mp3Path", eElement);
                        String MainTxt = getTagValue("MainTxt", eElement);
                        String paperTitle = getTagValue("paperTitle", eElement);
                        
                        speakingQuestion sq = new speakingQuestion();
                        sq.setMainTxt(MainTxt);
                        sq.setMp3Path(mp3Path);
                        sq.setPaperTitle(paperTitle);
                        sq.setPrepareTime(prepareTime);
                        sq.setReadingTime(readingTime);
                                
                        pxml.addSpeakingQuestionToPaper(sq);
                    }
                }
            }
        }catch(Exception ex){
            ex.printStackTrace();
        }
        return (speakingXML)pxml;
    }
    
    
    private static String getTagValue(String sTag, Element eElement) {
	NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();
        Node nValue = (Node) nlList.item(0);
        if(nValue == null)return "";
        String retValue = nValue.getNodeValue();
        return retValue;
    }
    
    private String fileName;
}
